/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

window.addEventListener('load',init,false);
var canvas=null,ctx=null;
var lastPress=null;
var pause=false;
var gameover=true;
var dir=0;
var j=50;
var score=0;
var player=new Rectangle(40,40,30,30);
var cuadrojo=new Rectangle(80,80,30,30);
var pared=new Array();
   var arriba = false;
    var abajo = false;
    var izquierda = false;
    var derecha = false;
for (var i=1;i<=15;i++) {   
    pared.push(new Rectangle(j,450,30,30));
    j+=30;
};
pared.push(new Rectangle(200,50,30,30));
pared.push(new Rectangle(200,300,30,30));
pared.push(new Rectangle(300,50,30,30));
pared.push(new Rectangle(300,300,30,30));

var KEY_ENTER=13;


function random(max){
    return Math.floor(Math.random()*max);
}

function init(){
    canvas=document.getElementById('canvas');
    ctx=canvas.getContext('2d');
    run();
    repaint();
}

function run(){
    setTimeout(run,50);
    act();
}

function repaint(){
    requestAnimationFrame(repaint);
    paint(ctx);
}

function reset(){  ///es como queremos que inicie el juego
    score=0;
    dir=1;
    player.x=40;
    player.y=40;
    cuadrojo.x=random(canvas.width/10-1)*10;
    cuadrojo.y=random(canvas.height/10-1)*10;
    gameover=false;
}
          function pulsar(e){
            switch(e.keyCode){
                    //izquierda
                    case 37:
                            izquierda = true;
                            break;
                    //arriba
                    case 38:
                            arriba = true;
                            break;
                    //derecha
                    case 39:
                            derecha = true;
                            break;
                    //abajo
                    case 40:
                            abajo = true;
                            break;
            }
    }
    function soltar(e){
	switch(e.keyCode){
		//izquierda
		case 37:
			izquierda = false;
			break;
		//arriba
		case 38:
			arriba = false;
			break;
		//derecha
		case 39:
			derecha = false;
			break;
		//abajo
		case 40:
			abajo = false;
			break;
	}
}
function act(){
  //  if(!pause){
        // GameOver Reset
        if(gameover)
            reset();
        if (izquierda && player.x >= 15){
		player.x-=15;
	}
	//si vamos a la derecha y podemos sumarle 15 sin pasarno del ancho de la pagina
	if (derecha && player.x+30 <= (canvas.width - 15) ){
		player.x+=15;
	}
	if (arriba && player.y>= 15){
            player.y-=15;
	}
	if (abajo && player.y+30 <= (canvas.height - 15) ){
		player.y+=15;
	}
        // Change Direction
        /*if(lastPress===KEY_UP)
            dir=0;
        if(lastPress===KEY_RIGHT)
            dir=1;
        if(lastPress===KEY_DOWN)
            dir=2;
        if(lastPress===KEY_LEFT)
            dir=3;

        // Move Rect
        if(dir===0)
            player.y-=10;
        if(dir===1)
            player.x+=10;
        if(dir===2)
            player.y+=10;
        if(dir===3)
            player.x-=10;*/
        


        // Out Screen
     /*   if(player.x>canvas.width)
            player.x=0;
        if(player.y>canvas.height)
            player.y=0;
        if(player.x<0)
            player.x=canvas.width;
        if(player.y<0)
            player.y=canvas.height;*/
        
        // Colision entre jugador y rect rojo
        if(player.intersects(cuadrojo)){
            console.log("choque con cuadrado rojo");
            //lo vuelvo a posisionar
            player.x = cuadrojo.x -30;
            //cuadrojo.x=random(canvas.width/10-1)*10;
            //cuadrojo.y=random(canvas.height/10-1)*10;
        }
        
        // Interseccion de pared con comida o jugador
        for(var i=0,l=pared.length;i<l;i++){
            if(cuadrojo.intersects(pared[i])){
                cuadrojo.x=random(canvas.width/10-1)*10;
                cuadrojo.y=random(canvas.height/10-1)*10;
            }
            
            if(player.intersects(pared[i])){
             console.log("choco con pared"); 
             
            //gameover=true;
            }
         
        }
  //  }
    // Pause/Unpause
    if(lastPress===KEY_ENTER){
        pause=!pause;
        lastPress=null;
    }
}

function paint(ctx){
    ctx.fillStyle='#000';
    ctx.fillRect(0,0,canvas.width,canvas.height);
    ctx.fillStyle='#0f0';
    player.fill(ctx);
    ctx.fillStyle='#999';
    
    //dibujo paredes
    for(var i=0,l=pared.length;i<l;i++){
        pared[i].fill(ctx);
    }
    ctx.fillStyle='#f00';
    cuadrojo.fill(ctx);
    
    ctx.fillStyle='#fff';
    //ctx.fillText('Last Press: '+lastPress,0,20);
    ctx.fillText('Score: '+score,0,10);
    if(pause){
        ctx.textAlign='center';
        if(gameover)
            ctx.fillText('GAME OVER',150,75);
        else
            ctx.fillText('PAUSE',150,75);
        ctx.textAlign='left';
    }
}


window.addEventListener('keydown',pulsar,false);
window.addEventListener('keyup',soltar,false);


function Rectangle(x,y,width,height){
    this.x=(x===null)?0:x;
    this.y=(y===null)?0:y;
    this.width=(width===null)?0:width;
    this.height=(height===null)?this.width:height;
    
    ///funcion de interseccion
    
    this.intersects=function(rect){
        if(rect!==null){
            return(this.x<rect.x+rect.width&&
                this.x+this.width>rect.x&&
                this.y<rect.y+rect.height&&
                this.y+this.height>rect.y);
        }
    }
    
    this.fill=function(ctx){
        if(ctx!==null){
            ctx.fillRect(this.x,this.y,this.width,this.height);
        }
    }
}

//window.requestAnimationFrame=(function(){
 //   return window.requestAnimationFrame || 
  //      window.webkitRequestAnimationFrame || 
 // /      window.mozRequestAnimationFrame || 
 //       function(callback){window.setTimeout(callback,17);};
//})
//();
